const app = getApp();
var util = require('../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    content:'',
    time: 30,    //倒计时
    numshow: true,  // ‘发送’字样
    setInter: '',  //定时器
    isphone_test:false,
    imgPath:'',
    info:{},
    KeyboardKeys: [1, 2, 3, 4, 5, 6, 7, 8, 9, '·', 0], //键盘
    uploadImgCount:0,
    iscopy:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(){
    var that=this;
    app.AppHttp('api/Client/basicInfo', {
      openid: app.globalData.openid
    }, function (res) {
      if(res.data.phone.length == 11 && res.data.client_phone==res.data.phone && res.data.is_manage==1){  //对比电话和管理员身份
        util.again_login();
      };
      that.setData({
        info: res.data,
        tel: res.data.phone.substr(0, 3) + '****' + res.data.phone.substr(7)
      });
    })
  },
  // 更改全称或简称
  change_name(e){  
    var update_name=e.currentTarget.dataset.update_name;
    wx.navigateTo({
      url:'inputcompanyname/inputcompanyname?update_name='+update_name+'&nametype='+e.currentTarget.dataset.nametype
    })
  },
  bindChooiceProduct(){
    wx.navigateTo({
      url: '/pages/cropper/cropper?imgx=1',
    });
  }, 
  // 更换密码
  change_pwd(){
    util.showBuyModal(this);
    this.setData({
      isphone_test:true
    });
  },
  // 关闭验证弹窗
  close_pwd(){
    util.hideBuyModal(this,'isphone_test');
  },
  // 获取验证码
  getcode(event) {
    var that = this;
    var times = that.times
    wx.request({
      url: app.globalData.url + 'api/Client/AjaxVerificationCode',
      method: "POST",
      data: {
        phone: that.data.info.phone
      },
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
          duration: 500
        }, 2000);
      }
    });
    that.setData({
      numshow: false
    });
    that.data.setInter = setInterval(times, 1000);
  },
  // 倒计时
  times(){
    var time = this.data.time;
    time--;
    if (time == 0) {
      this.setData({
        numshow: true,
        time: 10
      });
      clearInterval(this.data.setInter);
      return;
    }
    this.setData({
      time: time
    });
  },
  // 显示粘贴
  showcopy(){
    var iscopy=this.data.iscopy;
    if(iscopy<2){
      iscopy++
    }else{
      iscopy=0;
    }
    this.setData({
      iscopy:iscopy
    });
  },
  // 隐藏粘贴
  hidecopy(){
    this.setData({
      iscopy:0
    });
  },
  // 粘贴验证码
  tieText(){
    var that = this;
    wx.getClipboardData({
      success: function(res){
        if(res.data && (/^\d{4}$/).test(res.data)){
          that.setData({
            content:res.data,
            code:res.data,
            iscopy:0
          });
        };          
      }
    })
  },
  // 键盘按键
  keyTap(e) {
    util.keyfunction(e,this,3);
    this.setValue();
  },
  setValue(){
    this.setData({
      code: this.data.content
    });
  },
  // 确认更换密码
  change_pwd_conf(){
    var that = this;
    app.AppHttp('api/Client/checkPhoneCode', {
     phone:that.data.info.phone,
     client_id:app.globalData.loginInfo.id,
     code:that.data.code
    }, function (res) {
      if(res.data.status==1){
        util.hideBuyModal(that,'isphone_test');
        wx.showToast({
          title: res.data.msg,
          success:function(){
            wx.navigateTo({
              url: 'change_pwd/change_pwd'
            });
          }
        },500)        
      }else{
        wx.showLoading({
          title: res.data.msg,
          duration:800,
        })
      }      
    })
  },
  // 财务审核权限开关
  set_exam_change(){
    var that = this;
    app.AppHttp('api/Client/setExamine', {
      id: app.globalData.loginInfo.id,
      set_examine:that.data.info.set_examine
    }, function (res) {
      that.setData({
        ['info.set_examine']:that.data.info.set_examine==1?0:1
      })        
    });
  },
  // 设置地址
  set_address(){
    let oldaddr = this.data.info.address?this.data.info.address:'';
    wx.navigateTo({
      url:'inputaddress/inputaddress?oldaddr='+ oldaddr
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
  */
  onReady(){
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
    var that = this;
    if (app.globalData.imgSrc != '') {
      that.setData({
        imgSrc: app.globalData.imgSrc
      });
      wx.uploadFile({
        url: app.globalData.url + 'api/Client/editLogo',
        filePath: that.data.imgSrc,
        name: 'logo',
        method: "POST",
        formData: {
          openid: app.globalData.openid
        },
        success: function (res) {
          that.onLoad()
        },
        fail: function (res) {
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false,
          })
        }
      });
    }
    that.onLoad()
  },
  // 长按复制
  copyText(e) {
    let data = this.data.info.search_code
    if (data) {
      wx.setClipboardData({
        data: data,
        success: function(res) {
          wx.getClipboardData({
            success: function(res) {
              wx.showToast({
                title: '复制成功',
                icon: "success",
                duration: 1000
              })
            }
          })
        }
      })
    }else{
      wx.showToast({
        title: '复制失败',
        icon: "none",
        duration: 1000
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide(){

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload(){
    clearInterval(this.data.setInter)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh(){
    this.onShow();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(){

  }
})